package com.reds.backend.service.impl.check;

import com.alibaba.fastjson2.JSON;
import com.reds.backend.pojo.Article;
import com.reds.backend.utils.adminUtils.admin.StpAdminUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.reds.backend.service.check.checkCRUDSerivce;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import com.reds.backend.mapper.adminMapper;
@Service
public class checkCRUDServiceImpl implements checkCRUDSerivce{
    @Autowired
    public RestTemplate template;
    @Autowired
    public adminMapper adminMapper;
    private String addUrl = "http://124.221.73.180:3002/check/addArticle/";
    private String checkUrl="http://124.221.73.180:3002/check/checkArticle/";
    private String authorUrl="http://124.221.73.180:3002/check/getAuthor/";
    private String deteleUrl="http://124.221.73.180:3002/check/deteleArticle/";
    private String updataUrl="http://124.221.73.180:3002/check/updataArticle/";
    @Override
    public String addArticle(Article article,String address) {
        MultiValueMap<String,String> map = new LinkedMultiValueMap<>();
        map.add("article", JSON.toJSONString(article));
        map.add("address",address);
        template.postForObject(addUrl,
                map,
                String.class);
        return "success";
    }

    @Override
    public String checkArticle(int id, String state) {
        String admin = adminMapper.selectById(StpAdminUtil.getLoginIdAsInt()).getName();
        MultiValueMap<String,String> map = new LinkedMultiValueMap<>();
        map.add("id", id+"");
        map.add("state",state);
        map.add("admin",admin);
        template.postForObject(checkUrl,
                map,
                String.class);
        return "success";
    }

    @Override
    public String getAuthor(int id) {
        MultiValueMap<String,String> map = new LinkedMultiValueMap<>();
        map.add("id",id+"");
        template.postForObject(authorUrl,
                map,
                String.class);
        return "success";
    }

    @Override
    public String deteleArticle(int id) {
        MultiValueMap<String,String> map = new LinkedMultiValueMap<>();
        map.add("id",id+"");
        template.postForObject(deteleUrl,
                map,
                String.class);
        return "success";
    }

    @Override
    public String updataArticle(Article article,String address) {
        System.out.println(article);
        MultiValueMap<String,String> map = new LinkedMultiValueMap<>();
        map.add("article",JSON.toJSONString(article));
        map.add("address",address);
        template.postForObject(updataUrl,
                map,
                String.class);
        return "success";
    }
}
